new Vue({
  el: '#app',
  data() {
    return {
      form: {
        page: 1,			//页数
        limit: 10			//每页数据条数
      },
      total: 0,
      loading: false,//节流
      Warehouse: {},//
      fileXlsx: [],//表格
      table: {
        Warehouse_SerialNumber:"",
        Applicable_Type:"",
        Parent_Warehouse_SerialNumber: "",
        token: $.cookie("token"),
        Enterprise_Info_SerialNumber: $.cookie("enterprise"),
        List: []
      }
    }
  },
  mounted() {
    let obj = JSON.parse(sessionStorage.getItem('Initialization'))
    console.log(obj);
    this.table.Warehouse_SerialNumber = obj.Warehouse_SerialNumber;
    this.table.Applicable_Type = obj.Applicable_Type;
    this.table.Parent_Warehouse_SerialNumber = obj.Parent_Warehouse_SerialNumber;
  },
  methods: {
    onSerach() {			//查询方法
      this.form.page = 1;
    },
    handleSizeChange(val) {
      this.form.page = 1;
      this.form.limit = val;
    },
    handleCurrentChange(val) {
      this.form.page = val;
    },
    uploadXLSX(file) {
      this.fileXlsx.push({
        name: file.name,
        url: file.type
      })
      const _this = this
      var reader = new FileReader();
      reader.readAsBinaryString(file);
      reader.onload = function (e) {
        var data = e.target.result;
        var wb = XLSX.read(data, {
          type: 'binary' //以二进制的方式读取
        });
        var sheet = wb.Sheets[wb.SheetNames[0]];//sheet0代表excel表格中的第一页
        var json = XLSX.utils.sheet_to_json(sheet);//利用接口实现转换。
        json.forEach(item => {
          let obj = {
            Article_SerialNumber: item['商品id'],
            Inventory_Total_Quantity: item['入库数量'],
            Amount: item['金额'],
            Article_Name: item['商品名称'],
            Enterprise_Info_SerialNumber: $.cookie("enterprise"),
            Expiration_Date: getYMDHMSDate()
          }
          _this.table.List.push(obj)
        })
        _this.total = _this.table.List.length
        console.log(_this.table);
      }
    },
    submitForm() {
      let this_ = this;
      this.loading = true
      $.ajax({
        type: 'POST',
        url:`${DomainName}/addRoleInfo`,
        data:{
          Data:JSON.stringify(this.table),
          URL:"/Inventory/initWarehouse"
        },
        complete: function (XMLHttpRequest) {
          this_.loading = true
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        success: function (Result) {
          if (Result.data.length>0) {
            this_.table.List = this_.table.List.filter(item => !Result.data.includes(item.Article_SerialNumber))
            this_.$message.warning("存在未知商品，请先在商品信息中录入该商品")
          }else{
            this_.$message.success("导入成功");
            window.history.go(-1);
          }
        },
        dataType: "json"
      });
    },
    XlsxbeforeRemove(){
      
    }
  },
})